package com.sjsu.aws.configurationMgmt;

import java.util.HashMap;
import java.util.Map;
import org.apache.log4j.Logger;
import com.amazonaws.services.dynamodb.model.AttributeValue;
import com.amazonaws.services.dynamodb.model.ComparisonOperator;
import com.amazonaws.services.dynamodb.model.Condition;
import com.amazonaws.services.dynamodb.model.ScanResult;
import com.sjsu.aws.db.QueryCriteria;
import com.sjsu.aws.db.TableOperationImpl;
import com.sjsu.aws.db.TableRow;

/**
 * This class manages Configuration management operations
 * @author Deepali Pawar
 * 
 */
public class ConfigMgmtServiceImpl implements ConfigMgmtService {
	private final static Logger	LOG	= Logger.getLogger(ConfigMgmtServiceImpl.class);
	
	private TableOperationImpl	        tableOperationImpl;
	private TableRow	                tableRow;
	private QueryCriteria	            criteria;
	
	public ConfigMgmtServiceImpl() throws Exception {		
		this.tableOperationImpl = new TableOperationImpl();
		this.tableRow = new TableRow();
		this.criteria = new QueryCriteria();		
	}
	
	@Override
	public void createConfig(ConfigMgmt cfg) throws Exception {
		LOG.info("Creation of Configuration");
		tableRow.setItem(createConfigMgmtItem(cfg));
		tableOperationImpl.insertItem(ConfigMgmtConstants.CONFIG_MGMT_TABLE,
		        tableRow);
	}
	
	@Override
	public void updateConfig(ConfigMgmt cfg) throws Exception {
		LOG.info("Updating Configuration");
		tableRow.setUpdateKey(cfg.getConfigId());
		tableRow.setUpdateColumnName(ConfigMgmtConstants.COLUMN_DATADIR);
		tableRow.setUpdateColumnValue(cfg.getDataDir());
		tableOperationImpl.UpdatePutItem(ConfigMgmtConstants.CONFIG_MGMT_TABLE,
		        tableRow);
	}
	
	@Override	
	public void readConfig(ConfigMgmt cfg) throws Exception {
		LOG.info("Reading Configuration");
		HashMap<String, Condition> scanFilter = new HashMap<String, Condition>();
		// Create condition
		Condition condition = new Condition().withComparisonOperator(
		        ComparisonOperator.EQ.toString()).withAttributeValueList(
		        new AttributeValue().withS(cfg.getConfigId()));
		
		scanFilter.put(ConfigMgmtConstants.COLUMN_CONFIGID, condition);
		
		Condition conditionCfgNodeId = new Condition()
		        .withComparisonOperator(ComparisonOperator.NOT_NULL);
		scanFilter.put(ConfigMgmtConstants.COLUMN_CONFIGID, conditionCfgNodeId);
		
		//Scan db
		criteria.setScanFilter(scanFilter);
		ScanResult scanResult = tableOperationImpl.queryTable(
		        ConfigMgmtConstants.CONFIG_MGMT_TABLE, criteria);
		
		for (int i = 0; i < scanResult.getCount(); i++) {
			HashMap<String, AttributeValue> item = (HashMap<String, AttributeValue>) scanResult
			        .getItems().get(i);
			if (item.get(ConfigMgmtConstants.COLUMN_CONFIGID).getS() != null) {
				LOG.info("Configuration Value is "
				        + item.get(ConfigMgmtConstants.COLUMN_DATADIR).getS());
			}
		}
		
	}
	
	/**
	 * 
	 * @param cfg
	 * @return map 
	 * @throws Exception
	 */
	private Map<String, AttributeValue> createConfigMgmtItem(ConfigMgmt cfg)
	        throws Exception {
		LOG.info("Creating Configuration Object");
		Map<String, AttributeValue> item = new HashMap<String, AttributeValue>();
		item.put(ConfigMgmtConstants.COLUMN_CONFIGID,
		        new AttributeValue(cfg.getConfigId()));
		item.put(ConfigMgmtConstants.COLUMN_DATADIR,
		        new AttributeValue(cfg.getDataDir()));
		item.put(ConfigMgmtConstants.COLUMN_CLIENTPORT,
		        new AttributeValue(cfg.getClientPort()));
		return item;
	}
}
